package com.zcs.app.wisdomschool.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.greateffect.littlebud.lib.adapter.CommonRecyclerAdapter;
import com.greateffect.littlebud.lib.base.BaseNormalActivityAdv;
import com.greateffect.littlebud.lib.cache.CustomExtraKeys;
import com.greateffect.littlebud.lib.imageloader.ImageLoader;
import com.greateffect.littlebud.lib.provider.PicProvider;
import com.greateffect.littlebud.lib.utils.GlideStaticUtils;
import com.greateffect.littlebud.lib.utils.JColors;
import com.greateffect.littlebud.lib.utils.JListKit;
import com.zcs.app.wisdomschool.DataProvider;
import com.zcs.app.wisdomschool.R;
import com.zcs.app.wisdomschool.bean.EvaluateBean;
import com.zcs.autolayout.utils.AutoUtils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.ViewById;

import java.util.List;

/**
 * Created by ZengCS on 2019/1/18.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
@EActivity(R.layout.activity_process_list)
public class ProcessListActivity extends BaseNormalActivityAdv {
    @Extra(CustomExtraKeys.KEY_INTEGER)
    int mPosition;
    // 请修改对应标题
    private static final String TITLE = "流程列表";
    // 以下代码请勿修改
    @ViewById(R.id.id_tv_custom_toolbar_title)
    TextView tvTitle;
    @ViewById(R.id.id_top_indicator)
    View mIndicator;
    @ViewById(R.id.id_tv_tab_1)
    TextView tab1;
    @ViewById(R.id.id_tv_tab_2)
    TextView tab2;
    @ViewById(R.id.id_tv_tab_3)
    TextView tab3;
    @ViewById(R.id.id_tv_tab_4)
    TextView tab4;

    @ViewById(R.id.id_rv_process)
    RecyclerView mRecyclerView;

    private List<EvaluateBean> mItems = JListKit.newArrayList();
    private CommonRecyclerAdapter<EvaluateBean> mAdapter;

    @AfterViews
    void init() {
        // showToast("mPosition = " + mPosition);
        tvTitle.setText(TITLE);
        // 设置Toolbar顶部Padding
        setToolbarPaddingWhenTransStatusBar();

        moveIndicator(true);
    }

    private void refreshData() {
        mItems.clear();
        switch (mPosition) {
            case 0:// 全部
                mItems.addAll(DataProvider.getAll());
                break;
            case 1:// 日常事务
                mItems.addAll(DataProvider.getDailyList());
                break;
            case 2:// 评奖评优
                mItems.addAll(DataProvider.getAwardList());
                break;
            case 3:// 资助评定
                mItems.addAll(DataProvider.getEvaluateList());
                break;
        }

        initAdapter();
    }

    private void initAdapter() {
        if (mAdapter == null) {
            mAdapter = new CommonRecyclerAdapter<EvaluateBean>(R.layout.item_evaluate_adv, mItems) {
                @Override
                protected void convert(BaseViewHolder helper, EvaluateBean item) {
                    helper.setText(R.id.id_tv_evaluate_name, item.getName());
                    // GlideStaticUtils.displayImage(item.getPic(), R.drawable.empty_photo, helper.getView(R.id.id_iv_evaluate_pic));
                    helper.setImageResource(R.id.id_iv_evaluate_pic, item.getIcon());
                    TextView btnApply = helper.getView(R.id.id_btn_apply);
                    btnApply.setText(item.isCanApply() ? "申请" : "查看");
                    btnApply.setEnabled(item.isCanApply());
                    helper.setGone(R.id.id_iv_free, item.isCanApply());

                    helper.getView(R.id.id_btn_apply).setOnClickListener(view -> {
                        EvaluateBean bean = mItems.get(helper.getAdapterPosition());
                        openApplyActivity(bean);
                    });
                }
            };
            View footView = LayoutInflater.from(getAttachedContext()).inflate(R.layout.simple_foot_view, null, false);
            mAdapter.addFooterView(footView);
            mAdapter.setOnItemClickListener((adapter, view, position) -> {
                try {
                    EvaluateBean bean = mItems.get(position);
                    openApplyActivity(bean);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getAttachedContext()));
        } else {
            mAdapter.setNewData(mItems);
            mRecyclerView.smoothScrollToPosition(0);
        }
    }

    // 打开申请页面
    private void openApplyActivity(EvaluateBean bean) {
        Intent intent = new Intent(this, ApplyDetailActivity_.class);
        intent.putExtra(CustomExtraKeys.KEY_SERIALIZABLE_BEAN, bean);
        launchActivity(intent);
    }

    private boolean isAnimating = false;

    private void moveIndicator(boolean isInit) {
        if (isAnimating) return;
        float x0 = mIndicator.getTranslationX();
        float x1 = AutoUtils.getPercentWidthSize((int) (mPosition * 750 / 4f));
        if (isInit) {
            mIndicator.setTranslationX(x1);
            onAnimationEnd();
        } else {
            isAnimating = true;
            ObjectAnimator anim = ObjectAnimator.ofFloat(mIndicator, "translationX", x0, x1);
            anim.setDuration(200);
            anim.start();
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    ProcessListActivity.this.onAnimationEnd();
                }
            });
        }
    }

    private void onAnimationEnd() {
        isAnimating = false;
        tab1.setTextColor(mPosition == 0 ? JColors.colorPrimary : 0xff818181);
        tab2.setTextColor(mPosition == 1 ? JColors.colorPrimary : 0xff818181);
        tab3.setTextColor(mPosition == 2 ? JColors.colorPrimary : 0xff818181);
        tab4.setTextColor(mPosition == 3 ? JColors.colorPrimary : 0xff818181);
        refreshData();
    }

    // 处理点击事件
    @Click({
            R.id.id_iv_custom_toolbar_back,
            R.id.id_tv_tab_1,
            R.id.id_tv_tab_2,
            R.id.id_tv_tab_3,
            R.id.id_tv_tab_4,
    })
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_tv_tab_1:
                if (mPosition == 0) return;
                mPosition = 0;
                moveIndicator(false);
                break;
            case R.id.id_tv_tab_2:
                if (mPosition == 1) return;
                mPosition = 1;
                moveIndicator(false);
                break;
            case R.id.id_tv_tab_3:
                if (mPosition == 2) return;
                mPosition = 2;
                moveIndicator(false);
                break;
            case R.id.id_tv_tab_4:
                if (mPosition == 3) return;
                mPosition = 3;
                moveIndicator(false);
                break;
            case R.id.id_iv_custom_toolbar_back:
                onBackPressed();
                break;
        }
    }
}
